!-------------------------------------------------------------LICENSE--------------------------------------------------------------!
!                                                                                                                                  !
!The MAP code is written in Fortran language for magnetohydrodynamics (MHD) calculation with the adaptive mesh refinement (AMR)    !
!and Message Passing Interface (MPI) parallelization.                                                                              !
!                                                                                                                                  !
!Copyright (C) 2012                                                                                                                !
!Ronglin Jiang                                                                                                                     !
!rljiang@ssc.net.cn                                                                                                                !
!585 Guoshoujing Road. Pudong, Shanghai, P.R.C. 201203                                                                             !
!                                                                                                                                  !
!This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License         !
!as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.             !
!                                                                                                                                  !
!This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of    !
!MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.                        !
!                                                                                                                                  !
!You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software     !
!Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.                                                   !
!                                                                                                                                  !
!-------------------------------------------------------------LICENSE--------------------------------------------------------------!

recursive subroutine integrate (current)

   use parameters
   use block_define

   implicit none

   type(block), pointer, intent(inout) :: current

   integer(4) :: i, i_p, i_c, i_block, nx_h
   type(block), pointer :: child, parent

!----------------------------------------------------------------------------------------------------------------------------------------------|
   if (associated (current%child)) then
      do i_block = 1, current%nc
         child => current%child(i_block)
         call integrate (child)
      enddo
   else
      select case(scheme)
      case (1)
         call tvd_mmc (current%ro, current%mx, current%my, current%mz, current%bx, current%by, current%bz, current%en,             &
            current%nx, ng, current%flux_bnd, n_neighs, sys, current%dx, min_dt, gm, max_speed)
      case (2)
         call tvd_lf (current%ro, current%mx, current%my, current%mz, current%bx, current%by, current%bz, current%en,              &
            current%nx, ng, current%flux_bnd, n_neighs, sys, current%dx, min_dt, gm, riemann_solver_flag)
      case (3)
         call weno (current%ro, current%mx, current%my, current%mz, current%bx, current%by, current%bz, current%en,                &
            current%nx, ng, current%flux_bnd, n_neighs, sys, current%dx, min_dt, gm, riemann_solver_flag)
      case (4)
         call ucs (current%ro, current%mx, current%my, current%mz, current%bx, current%by, current%bz, current%en,                 &
            current%nx, ng, current%flux_bnd, n_neighs, sys, current%dx, min_dt, gm)
      end select
   endif

   if (current%lv .gt. 1) then
      parent => current%pp
      nx_h = nx_no_bnd / parent%nc
      do i = 1, nx_h
         i_p = i + nx_h * (current%p_pos - 1) + ng
         i_c = i * 2 + ng
         parent%ro(i_p) = (current%ro(i_c - 1) + current%ro(i_c)) / 2.0d0
         parent%mx(i_p) = (current%mx(i_c - 1) + current%mx(i_c)) / 2.0d0
         parent%my(i_p) = (current%my(i_c - 1) + current%my(i_c)) / 2.0d0
         parent%mz(i_p) = (current%mz(i_c - 1) + current%mz(i_c)) / 2.0d0
         parent%bx(i_p) = (current%bx(i_c - 1) + current%bx(i_c)) / 2.0d0
         parent%by(i_p) = (current%by(i_c - 1) + current%by(i_c)) / 2.0d0
         parent%bz(i_p) = (current%bz(i_c - 1) + current%bz(i_c)) / 2.0d0
         parent%en(i_p) = (current%en(i_c - 1) + current%en(i_c)) / 2.0d0
      enddo
   endif

   current%flag_skip = 0
   current%t = current%t + min_dt

   return
end subroutine integrate
